import * as THREE from "three";
export default function (option) {
  const { source, target, height, size, color, range, speed = 1 } = option;
  const positions = [];
  const attrPositions = [];
  const attrCnumber = [];

  const _source = new THREE.Vector3(source.x, source.y, source.z);
  const _target = new THREE.Vector3(target.x, target.y, target.z);
  const _center = _target.clone().lerp(_source, 0.5); //取中间位置的向量
  _center.y += height;

  let number = parseInt(
    _source.distanceTo(_center) + _target.distanceTo(_center)
  );
  number = number * 10;
//   console.log("number", number);
  const curve = new THREE.QuadraticBezierCurve3(_source, _center, _target);

  const points = curve.getPoints(number);

  // 粒子位置计算

  points.forEach((elem, i) => {
    positions.push({
      x: elem.x,
      y: elem.y,
      z: elem.z,
    });
    attrCnumber.push(i);
  });

  // console.log('attrCnumber',attrCnumber);
  positions.forEach((p) => {
    attrPositions.push(p.x, p.y, p.z);
  });

  const geometry = new THREE.BufferGeometry();

  geometry.setAttribute(
    "position",
    new THREE.Float32BufferAttribute(attrPositions, 3)
  );
  // 传递当前所在位置
  geometry.setAttribute(
    "current",
    new THREE.Float32BufferAttribute(attrCnumber, 1)
  );

  const shader = new THREE.ShaderMaterial({
    transparent: true,
    depthWrite: false,
    depthTest: false,
    blending: THREE.AdditiveBlending,
    uniforms: {
      uColor: {
        value: new THREE.Color(color), // 颜色
      },
      uRange: {
        value: range || 100, // 显示当前范围的个数
      },
      uSize: {
        value: size, // 粒子大小
      },
      uTotal: {
        value: number, // 当前粒子的所有的总数
      },
      uSpeed: {
        value: speed, // 当前粒子的所有的总数
      },
      time: {
        value: 0, //
      },
    },
    vertexShader: `
        attribute float current;
        uniform float time;
        uniform float uSize;
        uniform float uRange; // 展示区间
        uniform float uTotal; // 粒子总数
        uniform vec3 uColor; 
        uniform float uSpeed; 
        varying vec3 vColor;
        varying float vOpacity;
        void main() {
            // 需要当前显示的索引
            float size = uSize;
            float showNumber = uTotal * mod(time*uSpeed, 1.0); // [0,uTotal)
            if (showNumber > current && showNumber < current + uRange) {
                float uIndex = ((current + uRange) - showNumber) / uRange;
                size *= uIndex;
                vOpacity = 1.0;
            } else {
                vOpacity = 0.0;
            }

            // 顶点着色器计算后的Position
            vColor = uColor;
            gl_Position = projectionMatrix *  modelViewMatrix * vec4(position, 1.0); 
            // 大小
            gl_PointSize = size * 10.0 ;
        }`,
    fragmentShader: `
        varying vec3 vColor; 
        varying float vOpacity;
        void main() {
            gl_FragColor = vec4(vColor, vOpacity);
        }`,
  });

  const point = new THREE.Points(geometry, shader);

  return point;
}
